def divide_numbers(a, b):
    try:
        result = a / b
    except ZeroDivisionError as e:
        print(f"Error: {e}. Cannot divide by zero.")
    except TypeError as e:
        print(f"Error: {e}. Please provide valid numbers.")
    else:
        print(f"The result of division is: {result}")
    finally:
        print("Division operation completed.")

# 测试用例
divide_numbers(10, 2)  # 正常情况
divide_numbers(10, 0)  # 除零错误
divide_numbers(10, 'a')  # 类型错误

'''
输出结果：
The result of division is: 5.0
Division operation completed.
Error: division by zero. Cannot divide by zero.
Division operation completed.
Error: unsupported operand type(s) for /: 'int' and 'str'. Please provide valid numbers.
Division operation completed.
'''


# 自定义异常类
class NegativeNumberError(Exception):
    """当输入为负数时抛出此异常"""
    def __init__(self, value):
        self.value = value
        super().__init__(f"Negative number not allowed: {value}")

# 函数示例
def calculate_square_root(n):
    if n < 0:
        raise NegativeNumberError(n)
    return n ** 0.5

# 测试用例
try:
    result = calculate_square_root(-4)
except NegativeNumberError as e:
    print(f"Caught an exception: {e}")
else:
    print(f"The square root is: {result}")


def read_file(file_path):
    try:
        with open(file_path, 'r') as file:
            content = file.read()
            print("File content:")
            print(content)
    except FileNotFoundError as e:
        print(f"Error: The file '{file_path}' does not exist.")
    except PermissionError as e:
        print(f"Error: You do not have permission to read the file '{file_path}'.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
    finally:
        print("File operation attempted.")

# 测试用例
read_file('example.txt')  # 假设 example.txt 不存在